﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Globalization
Imports Newtonsoft.Json
Imports roncliProductions.LibWowArmory.Enums
Imports roncliProductions.LibWowArmory.Extensions
Imports roncliProductions.LibWowArmory.Internationalization

Namespace roncliProductions.LibWowArmory.Item

    ''' <summary>
    ''' A class that retrieves item information from the Blizzard WoW API.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ItemLookup
        Inherits WowArmoryData

        Private ilItem As New Schema.item

#Region "WowArmoryData Overrides"

#Region "Public Properties"

        ''' <summary>
        ''' The options for looking up an item.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Options As New ItemLookupOptions

        ''' <summary>
        ''' The length of time the data should be cached for, defaulting to 30 days for item information.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Property CacheLength As New TimeSpan(30, 0, 0, 0)

#End Region

#Region "Protected Properties"

        Protected Overrides ReadOnly Property CacheKey As String
            Get
                Return String.Format(CultureInfo.InvariantCulture, "LibWowArmory.Item.{0}", Options.ItemID)
            End Get
        End Property

        Protected Overrides ReadOnly Property URI As Uri
            Get
                Return New Uri(String.Format(CultureInfo.InvariantCulture, "/api/wow/item/{0}", Options.ItemID), UriKind.Relative)
            End Get
        End Property

#End Region

#End Region

#Region "Properties"

        Private iItem As Item
        ''' <summary>
        ''' The item returned from the Blizzard WoW API.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Item As Item
            Get
                Return iItem
            End Get
        End Property

#End Region

#Region "Methods"

        ''' <summary>
        ''' A default constructor to retrieve item information from the Blizzard WoW API.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' A constructor to retrieve item information for an item from the Blizzard WoW API.
        ''' </summary>
        ''' <param name="intItemID"></param>
        ''' <remarks></remarks>
        Public Sub New(intItemID As Integer)
            Options.ItemID = intItemID
            Load()
        End Sub

        ''' <summary>
        ''' A constructor to retrieve item information for a specific region and language from the Blizzard WoW API.
        ''' </summary>
        ''' <param name="rRegion"></param>
        ''' <param name="lLanguage"></param>
        ''' <remarks></remarks>
        Public Sub New(rRegion As Region, lLanguage As Language)
            Region = rRegion
            Language = lLanguage
        End Sub

        ''' <summary>
        ''' A constructor to retrieve item information for an item for a specific region and language from the Blizzard WoW API.
        ''' </summary>
        ''' <param name="rRegion"></param>
        ''' <param name="lLanguage"></param>
        ''' <param name="intItemID"></param>
        ''' <remarks></remarks>
        Public Sub New(rRegion As Region, lLanguage As Language, intItemID As Integer)
            Me.New(rRegion, lLanguage)
            Options.ItemID = intItemID
            Load()
        End Sub

        ''' <summary>
        ''' Loads the item.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()
            ilItem = CType(JsonConvert.DeserializeObject(Data, GetType(Schema.item)), Schema.item)
            iItem = New Item(
                ilItem.id,
                ilItem.name,
                ilItem.icon,
                CType(ilItem.quality, Quality),
                ilItem.itemClass.GetItemClass(Region, Language)
                )
        End Sub

#End Region

    End Class

End Namespace
